#!/bin/bash

function get_real_status()
{
  STATUS="realmStatus.aux"

  wget http://www.warcraftchina.com/action/system/$STATUS 1>/dev/null 2>&1

  # Print statistic
  cat $STATUS |\
    awk -F'[' '{print $NF}' |\
    awk -F']' '{print $1}' |\
    awk -F"},{" '{for (i=1;i<=NF;i++) print $i}'|\
    awk -F',' 'BEGIN{\
      my_server="\xE7\x93\xA6\xE9\x87\x8C\xE7\x8E\x9B\xE8\x90\xA8\xE6\x96\xAF";
      up_str="Up";
      up_nbr=0;
      dw_str="Down";
      dw_nbr=0;
    }{\
      gsub("{","",$0);\
      gsub("}","",$0);\
      gsub("\"","",$0);\
      split($1,ld,":"); load=ld[2];\
      if (length(load)==0) load="N/A";\
      split($2,nm,":"); name=nm[2];\
      split($4,st,":"); stat=st[2];\
      split($5,tp,":"); type=tp[2];\
      if (name ~ my_server){\
        my_stat=stat;\
        my_type=type;\
        my_load=load;\
      }\
      if (stat ~ up_str) up_nbr++;\
      if (stat ~ dw_str) dw_nbr++;\
    }END{\
      printf("Realm Status: Up=%s, Down=%s\n", up_nbr,dw_nbr);\
      printf("Varimathras is %s, load=%s\n",my_stat,my_load);\
    }'
  rm -f $STATUS
}

while [ 1 ]
do
  echo ""
  date
  get_real_status
  echo ""
  sleep 300
done

